// pages/mySelf/peopleInfo/peopleInfo.js
import http from "../../../utils/http"
import config from "../../../utils/config"
import Dialog from '@vant/weapp/dialog/dialog';
const common = require('../../../public.js')
Page({
  data: {
    userInfo: null,
    inputVal: "",
    show:true
  },
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('user') || null
    console.log(userInfo)
    if (userInfo != null) {
      this.setData({
        userInfo
      })
    }
  },
  //更换头像
  updHearImage() {
    let _this = this
    wx.chooseImage({
      success(res) {
        console.log(res);
        const tempFilePaths = res.tempFilePaths
      // const tempFilePaths =   wx.getFileSystemManager().readFileSync( res.tempFilePaths[0], "base64")
      console.log(tempFilePaths[0]) 
      wx.uploadFile({
          url: config.baseUrl + '/user/head/upload', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          header: {
            "Content-Type": "multipart/form-data"
          },
          name: 'uploadFile',
          formData: {
            userId: _this.data.userInfo.user.id,
            token: _this.data.userInfo.token,
            uploadFile: tempFilePaths[0]
          },
          success(res) {
            const data =JSON.parse( res.data)
            console.log(data);
            if(data.status == 200){
              let user = wx.getStorageSync('user')
              user.user.headImgurl = data.data.headImgurl
              wx.setStorageSync('user', user)
              setTimeout(()=>{
                  _this.setData({
                    userInfo:user
                  })
              },100)
            }
          }
        })
      }
    })
  },
  // 点击昵称
  diaNickname() {
    let userInfo = this.data.userInfo
    Dialog.confirm({
      title: '修改昵称',
      message: '弹窗内容',
    })
      .then(async () => {
        if (this.data.inputVal != '') {
          let option = {
            url: "/user/update",
            data: {
              userId: userInfo.user.id,
              token: userInfo.token,
              username: this.data.inputVal,
              headImgurl: userInfo.user.headImgurl
            },
          }
          let res = await http(option)
          console.log(res);
          if (res.data.status == 200) {
            wx.showToast({
              title: '修改成功',
            })
            let user = wx.getStorageSync('user')
            // user.user.alias = res.data.data.username
            user.user.username = res.data.data.username
            wx.setStorageSync('user', user)
            this.data.userInfo.user.username = res.data.data.username
            this.setData({
              userInfo: this.data.userInfo
            })
          } else {
            wx.showToast({
              title: res.data.msg,
            })
          }
          this.setData({
            inputVal: ""
          })
        } else {
          wx.showToast({
            title: '用户名不能为空',
            icon: "none"
          })
          return

        }
        // on confirm
      })
      .catch(() => {
        // on cancel
      });
  },
  //去实名验证页面
  toauthentication() {
    wx.navigateTo({
      url: '../authentication/authentication',
    })
  },
  //收货地址
  toadress() {
    wx.navigateTo({
      url: '/packageA/pages/Goods/Address/list/index'
    })
  },
  //查看微信绑定页面
  lookbinding() {
    wx.navigateTo({
      url: '../Accountbinding/Accountbinding',
    })
  },
  logout() {
    wx.removeStorageSync('user')
    common.showToast('您已退出登录')
    var pages = getCurrentPages();
    var beforePage = pages[pages.length - 2]; 
    beforePage.clearState();
    wx.navigateBack({  
      delta: 1,
      })
   
  }
})